// my页面数据文件
// 数据
// 后端的接口的定义文件
import Mock from 'mockjs';
// --------用户数据
const {userList} = Mock.mock({
    userList:[
        {
            name:"王师傅",
            userid:'10086',
            tel:'18677776666',
            avatar:'https://img1.baidu.com/it/u=3200425930,2413475553&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1732035600&t=12c608832d396c16be3e497c3a7f8515'
        }
    ]
})
// ---------订单数据
const {orders} = Mock.mock({
    orders:[
        {
            userid:'10086',
            orderNum:'2023121201',
            bgTime:'2023.12.12 19:00',
            endTime:'2023.12.20 13:14',
            payTime:'2023.12.20 14:00',
            payMode:'在线支付',
            orderMoney:'2000.00',
            shipper:{
                name:'张三',
                tel:'18022222222',
                address:'河北省石家庄市高新区物流园区88号仓库'
            },
            receiver:{
                name:'李四',
                tel:'18033333333',
                address:'北京市海淀区物流园区100号仓库'
            },
            cargoInformation:{
                type:'蔬菜',
                weight:'10吨',
                packingMethod:'箱装',
                size:'100方'
            },
            carInformation:{
                carNumber:'冀A00001',
                length:'4.2米',
                type:'平板',
                transportationType:'整车'
            },
            transportationMoney:'1000.00',
            otherNeed:'发票',
            remark:'早点到谢谢',
            orderState:'已完成'
        },
        {
            userid:'10086',
            orderNum:'2023121202',
            bgTime:'2023.12.12 19:00',
            endTime:'2023.12.20 13:14',
            payTime:'2023.12.20 14:00',
            payMode:'在线支付',
            orderMoney:'2000.00',
            shipper:{
                name:'张三',
                tel:'18022222222',
                address:'河北省石家庄市高新区物流园区88号仓库'
            },
            receiver:{
                name:'李四',
                tel:'18033333333',
                address:'北京市海淀区物流园区100号仓库'
            },
            cargoInformation:{
                type:'蔬菜',
                weight:'10吨',
                packingMethod:'箱装',
                size:'100方'
            },
            carInformation:{
                carNumber:'冀A00001',
                length:'4.2米',
                type:'平板',
                transportationType:'整车'
            },
            transportationMoney:'1000.00',
            otherNeed:'发票',
            remark:'早点到谢谢',
            orderState:'待装车'
        },
        {
            userid:'10086',
            orderNum:'2023121203',
            bgTime:'2023.12.12 19:00',
            endTime:'2023.12.20 13:14',
            payTime:'2023.12.20 14:00',
            payMode:'在线支付',
            orderMoney:'2000.00',
            shipper:{
                name:'张三',
                tel:'18022222222',
                address:'河北省石家庄市高新区物流园区88号仓库'
            },
            receiver:{
                name:'李四',
                tel:'18033333333',
                address:'北京市海淀区物流园区100号仓库'
            },
            cargoInformation:{
                type:'蔬菜',
                weight:'10吨',
                packingMethod:'箱装',
                size:'100方'
            },
            carInformation:{
                carNumber:'冀A00001',
                length:'4.2米',
                type:'平板',
                transportationType:'整车'
            },
            transportationMoney:'1000.00',
            otherNeed:'发票',
            remark:'早点到谢谢',
            orderState:'待付款'
        }
    ]
})


// 获取用户数据
Mock.mock('/api/getUserData','post',(config)=>{
    const req = JSON.parse(config.body)
    const user = JSON.parse(localStorage.getItem('userData'))
    // for (let i = 0; i < userList.length; i++) {
    //     if(userList[i].userid = req.userid){
    //         return{
    //             code:200,
    //             message:'获取数据成功',
    //             data:{
    //                 userData:userList[i]
    //             }
    //         }
    //     }
    // }
    if(user.userid = req.userid){
        return{
            code:200,
            message:'获取数据成功',
            data:{
                userData:user
            }
        }
    }
    
})
// 修改头像
Mock.mock('/api/setAvatarData','post',(config)=>{
    const req = JSON.parse(config.body)
    const user = JSON.parse(localStorage.getItem('userData'))
    user.avatar = req.avatar
    console.log("修改头像",user.avatar , req.avatar);
    
    localStorage.setItem('userData',JSON.stringify(user))
    return{
        code:200,
        message:'修改头像成功',
    }
})
// 初始加载，存储一下数据，后续修改注释掉
localStorage.setItem('userData',JSON.stringify(userList[0]))
localStorage.setItem('orderList',JSON.stringify(orders))
// 获取订单数据
Mock.mock('/api/getOrderListData','post',(config)=>{
    const orders = JSON.parse(localStorage.getItem('orderList'))
    const req = JSON.parse(config.body)
    const arr = []
    for (let i = 0; i < orders.length; i++) {
        if(req.userid == orders[i].userid){
            switch (req.type) {
                case '已完成':
                    if(req.type == orders[i].orderState){
                        arr.push(orders[i])
                    }  
                    break;
                case '待装车':
                    if(req.type == orders[i].orderState){
                        arr.push(orders[i])
                    }
                    break;
                case '运输中':
                    if(req.type == orders[i].orderState){
                        arr.push(orders[i])
                    }
                    break;
                case '待付款':
                    if(req.type == orders[i].orderState){
                        arr.push(orders[i])
                    }
                    break;
                default:
                    arr.push(orders[i])
                    break;
            }
        }
        
    }
    return{
        code:200,
        message:'查询数据成功',
        data:{
            order:arr
        }
    }
})

// 获取单一订单数据
Mock.mock('/api/getOrderData','post',(config)=>{
    const orders = JSON.parse(localStorage.getItem('orderList'))
    const req = JSON.parse(config.body)
    for (let i = 0; i < orders.length; i++) {
        if(req.orderNum == orders[i].orderNum){
            return{
                code:200,
                message:'查询成功',
                data:{
                    order:orders[i]
                }
            }
        }}
})

// 修改订单状态
Mock.mock('/api/changeStateData','post',(config)=>{
    const orders = JSON.parse(localStorage.getItem('orderList'))
    const req = JSON.parse(config.body)
    const state = ["全部", "待装车", "运输中", "待付款", "已完成"]
   
    console.log('下一个状态索引',1);
    
    for (let i = 0; i < orders.length; i++) {
        if(req.orderNum == orders[i].orderNum){
            orders[i].orderState = state[(state.indexOf(req.orderState) +1)]
        }
    }
    localStorage.setItem('orderList',JSON.stringify(orders))
    return{
        code:200,
        message:'修改状态成功',
        next:state[(state.indexOf(req.orderState) +1)]
    }
})
export {
	userList
}
